/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.account.service;

import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.AccountException;
import com.pitop.account.dao.AccountDAO;
import com.pitop.account.entity.AccountState;
import com.pitop.account.entity.Account;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.pitop.core.tools.security.Md5;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.HashMap;
import java.util.Date;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import com.baidu.fsg.uid.UidGenerator;

@Service("accountSV")
@Slf4j
public class AccountSVImpl extends BaseSVImpl<Account, Long> implements AccountSV {

    @Autowired
    private AccountDAO accountObjDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return accountObjDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     */
    @Override
    public void save(Account entity) throws BaseException {
        entity.setCode(String.valueOf(uidGenerator.getUID()));
        entity.setState(AccountState.Activate.name());
        entity.setPassword(Md5.md5(entity.getPassword()));
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        super.save(entity);
    }

    @Override
    public void modify(Account entity) throws BaseException {
        entity.setUpdateTime(new Date());
        super.modify(entity);
    }

    /**
     * 加载一个对象Account
     *
     * @param id      id*
     * @param code    编码*
     * @param account 账户
     * @return Account
     */
    @Override
    public Account load(java.lang.Long id, java.lang.String code, java.lang.String account) {
        if (id == null && code == null && account == null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        param.put("account", account);
        return accountObjDAO.load(param);
    }

    /**
     * 加载一个对象Account 通过id
     *
     * @param id id
     * @return Account
     */
    @Override
    public Account loadById(java.lang.Long id) {
        if (id == null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return accountObjDAO.loadById(id);
    }

    /**
     * 加载一个对象Account 通过code
     *
     * @param code 编码
     * @return Account
     */
    @Override
    public Account loadByCode(java.lang.String code) {
        if (code == null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return accountObjDAO.loadByCode(code);
    }

    /**
     * 加载一个对象Account 通过account
     *
     * @param account 账户
     * @return Account
     */
    @Override
    public Account loadByAccount(java.lang.String account) {
        if (account == null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return accountObjDAO.loadByAccount(account);
    }


    /**
     * 根据主键id,oldStates 共同更新 Account 的状态到newState状态
     *
     * @param id        id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(java.lang.Long id, AccountState newState, AccountState... oldStates) {
        if (id == null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        accountObjDAO.updateStateById(id, newState.name(), states);
    }

    /**
     * 根据主键code,oldStates 共同更新 Account 的状态到newState状态
     *
     * @param code      编码
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateByCode(java.lang.String code, AccountState newState, AccountState... oldStates) {
        if (code == null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        accountObjDAO.updateStateByCode(code, newState.name(), states);
    }

    /**
     * 根据主键account,oldStates 共同更新 Account 的状态到newState状态
     *
     * @param account   账户
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateByAccount(java.lang.String account, AccountState newState, AccountState... oldStates) {
        if (account == null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        accountObjDAO.updateStateByAccount(account, newState.name(), states);
    }

    /**
     * 根据主键id 更新 Account 的状态到另一个状态
     *
     * @param id    id
     * @param state 状态
     */
    @Override
    public void updateById(java.lang.Long id, AccountState state) {
        if (id == null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        accountObjDAO.updateById(id, state.name());
    }

    /**
     * 根据主键code 更新 Account 的状态到另一个状态
     *
     * @param code  编码
     * @param state 状态
     */
    @Override
    public void updateByCode(java.lang.String code, AccountState state) {
        if (code == null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        accountObjDAO.updateByCode(code, state.name());
    }

    /**
     * 根据主键account 更新 Account 的状态到另一个状态
     *
     * @param account 账户
     * @param state   状态
     */
    @Override
    public void updateByAccount(java.lang.String account, AccountState state) {
        if (account == null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        accountObjDAO.updateByAccount(account, state.name());
    }


    /**
     * 删除对象Account
     *
     * @param id      id*
     * @param code    编码*
     * @param account 账户
     */
    @Override
    public void delete(java.lang.Long id, java.lang.String code, java.lang.String account) {
        if (id == null && code == null && account == null) {
            throw new AccountException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        param.put("account", account);
        accountObjDAO.delete(param);
    }


    /**
     * 查询Account分页
     *
     * @param accountObj 对象
     * @param offset     查询开始行
     * @param limit      查询行数
     * @return List<Account>
     */
    @Override
    public List<Account> list(Account accountObj, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map;
        if (accountObj != null) {
            map = JSON.parseObject(JSON.toJSONString(accountObj, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashedMap();
        }
        return accountObjDAO.list(map, new RowBounds(offset, limit));
    }

    /**
     * 统计
     *
     * @param accountObj 账户信息
     * @return int
     */
    @Override
    public int count(Account accountObj) {
        Map<String, Object> map;
        if (accountObj != null) {
            map = JSON.parseObject(JSON.toJSONString(accountObj, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashedMap();
        }
        return accountObjDAO.count(map);
    }

    /**
     * 查询Account分页
     *
     * @param id     id* @param code  编码* @param account  账户
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<Account>
     */
    @Override
    public List<Account> list(java.lang.Long id, java.lang.String code, java.lang.String account, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        param.put("account", account);
        return accountObjDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Long id, java.lang.String code, java.lang.String account) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        param.put("account", account);
        return accountObjDAO.count(param);
    }

    /**
     * 根据账号和密码查询
     *
     * @param account  账号
     * @param password 密码
     * @return Account
     */
    @Override
    public Account loadByAccountAndPwd(String account, String password) {
        Map<String, String> paraMap = new HashMap<>();
        paraMap.put("account", account);
        paraMap.put("password", Md5.md5(password));
        return accountObjDAO.loadByAccountAndPwd(paraMap);
    }
}
